package com.igeek.health.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;

/**
 * @Description TODO
 * @Author 李国勇
 * @Date 2023/2/22 0:23
 */
//加载外部属性文件
@PropertySource("classpath:db.properties")
//Mapper接口的扫描包，为其生成代理对象，且注册至IOC容器
@MapperScan("com.igeek.health.mapper")
//开启注解事务  使用CGLIB动态代理
@EnableTransactionManagement(proxyTargetClass = true)

public class MyBatisConfig {
    //配置数据源
    @Bean
    public DataSource dataSource(
            @Value("${db.user}") String user,
            @Value("${db.password}") String password,
            @Value("${db.url}") String jdbcUrl,
            @Value("${db.driverClass}") String driverClass,
            @Value("${db.maxPoolSize}") int maxPoolSize
    ){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        dataSource.setUrl(jdbcUrl);
        dataSource.setDriverClassName(driverClass);
        dataSource.setMaxActive(maxPoolSize);
        return dataSource;
    }

    //配置事务管理器
    @Bean
    public TransactionManager transactionManager(DataSource dataSource){
        PlatformTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
        return transactionManager;
    }

    @Bean
    public SqlSessionFactoryBean factory(DataSource dataSource) throws IOException, IOException {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/*.xml"));
        factory.setTypeAliasesPackage("com.igeek.health.pojo");

        Configuration configuration = new Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        factory.setConfiguration(configuration);

        //分页插件拦截器
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect ","mysql");
        properties.setProperty("reasonable ","true");
        pageInterceptor.setProperties(properties);
        factory.setPlugins(new Interceptor[]{pageInterceptor});

        return factory;
    }
}
